import React, {Component, createRef} from 'react';


class Child extends Component {

  moneyRef = createRef()

  changeMoney = () => {
    this.props.changeMoney(this.moneyRef.current.value)
  }


  render() {
    return (
      <div>
        <input type="text" ref={this.moneyRef}/>
        <button onClick={this.changeMoney}>买一部手机</button>
      </div>
    );
  }
}


class GoodsList extends Component {


  state = {
    money: 1000000
  }


  changeMoney = (num) => {
    this.setState({
      money: this.state.money - num
    })
  }

  render() {
    return (
      <div>
        <h1>我有{this.state.money}元~~~</h1>
        <hr/>
        <Child changeMoney={this.changeMoney}/>
      </div>
    );
  }
}

export default GoodsList;
